package cn.moningxi.controller;

import cn.moningxi.bean.CLevelInfo;
import cn.moningxi.service.CLevelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CLevelInfoController {

    @Autowired
    private CLevelInfoService cLevelInfoService;

    /**
     * 查询出全部会员等级信息
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/allLevelInfo")
    public ModelAndView getLevelInfo(ModelAndView modelAndView) {
        List<CLevelInfo> levelInfoList = cLevelInfoService.queryAll();
        modelAndView.addObject("levelInfoList", levelInfoList);
        modelAndView.setViewName("backstage/member-level");
        return modelAndView;
    }

    /**
     * 跳转到修改会员信息界面
     *
     * @param modelAndView
     * @param levelId
     * @return
     */
    @RequestMapping("/allLevelInfo/editLevelInfo/{levelId}")
    public ModelAndView getCurrentEditLevelInfo(ModelAndView modelAndView, @PathVariable("levelId") Integer levelId) {
        CLevelInfo LevelInfo = cLevelInfoService.queryLevelInfoById(levelId);
        modelAndView.addObject("levelInfo", LevelInfo);
        modelAndView.setViewName("backstage/member-level-edit");
        return modelAndView;
    }

    /**
     * 将修改后的会员信息更新到数据库中
     *
     * @return
     */
    @RequestMapping(value = "/allLevelInfo/editLevelInfo/updateLevelInfo", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView updateLevelInfo(ModelAndView modelAndView, CLevelInfo cLevelInfo) {
        cLevelInfoService.updateLevelInfo(cLevelInfo);
        modelAndView.setViewName("redirect:/allLevelInfo");
        return modelAndView;
    }

    /**
     * 新增一个会员等级
     *
     * @param modelAndView
     * @param cLevelInfo
     * @return
     */
    @RequestMapping(value = "/allLevelInfo/addLevel", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView addLevel(ModelAndView modelAndView, CLevelInfo cLevelInfo) {
        cLevelInfoService.addLevel(cLevelInfo);
        modelAndView.setViewName("redirect:/allLevelInfo");
        return modelAndView;
    }

}
